package com.gzyj.jtt808.v2019.protocol;

import com.gzyj.jtt808.common.DriverInfo;
import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2013.enums.ICCardReadResult;
import com.gzyj.jtt808.v2013.enums.ICCardStatus;
import com.gzyj.jtt808.v2019.Package;
import com.gzyj.jtt808.v2019.enums.BusinessDataType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import lombok.Data;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * 驾驶员身份信息采集上报
 *
 * @author lhj
 */
@Data
public class JTT808_0x0702 extends Package implements Serializable {

    private ICCardStatus IC_Card_Status;//状态
    private String IC_Card_PlugDateTime;//插卡/拔卡时间
    private ICCardReadResult IC_Card_ReadResult;//IC卡读取结果

    private String DriverUserName;//驾驶员姓名

    private String QualificationCode;//从业资格证编码 20位
    private String LicenseIssuing;//发证机构名称

    private String CertificateExpiresDate;//证件有效期 BCD[4]

    private String DriverIdentityCard;//驾驶员身份证号 长度20 不足补 '\0'


    public JTT808_0x0702() {
        super(BusinessDataType.驾驶员身份信息采集上报.getCode());
    }

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat date_sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void encodeImpl(ByteBuf buf) {

        buf.writeByte(this.IC_Card_Status.getCode());
        try {
            buf.writeBytes(HexBytesUtil.WriteDateTime_yyMMddHHmmss(sdf.parse(this.IC_Card_PlugDateTime)));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        if (this.IC_Card_Status == ICCardStatus.从业资格证IC卡插入_驾驶员上班) {

            buf.writeByte(this.IC_Card_ReadResult.getCode());

            if (this.IC_Card_ReadResult == ICCardReadResult.IC卡读卡成功) {
                buf.writeByte(this.DriverUserName.getBytes(Charset.forName("GBK")).length);
                buf.writeCharSequence(this.DriverUserName, Charset.forName("GBK"));
                buf.writeBytes(HexBytesUtil.WriteStringPadLeft(this.QualificationCode, 20));
                buf.writeByte(this.LicenseIssuing.getBytes(Charset.forName("GBK")).length);
                buf.writeCharSequence(this.LicenseIssuing, Charset.forName("GBK"));
                try {
                    buf.writeBytes(HexBytesUtil.WriteDateTime_YYMMDD4(date_sdf.parse(this.CertificateExpiresDate)));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }

                buf.writeBytes(HexBytesUtil.WriteStringPadLeft(this.DriverIdentityCard, 20));
            }


        }


    }

    @Override
    public JTT808_0x0702 decodeImpl(ByteBuf buf) {

        String hex = ByteBufUtil.hexDump(buf).substring(0, 2);

        if (hex.equalsIgnoreCase("01") || hex.equalsIgnoreCase("02")) {
            this.IC_Card_Status = ICCardStatus.find(buf.readByte());
            this.IC_Card_PlugDateTime = HexBytesUtil.ReadDateTime_yyMMddHHmmss(HexBytesUtil.readByOffset(buf, 6));

            if (this.IC_Card_Status == ICCardStatus.从业资格证IC卡插入_驾驶员上班) {
                this.IC_Card_ReadResult = ICCardReadResult.find(buf.readByte());

                if (this.IC_Card_ReadResult == ICCardReadResult.IC卡读卡成功) {

                    int len = Byte.toUnsignedInt(buf.readByte());

                    this.DriverUserName = new String(HexBytesUtil.readByOffset(buf, len), Charset.forName("GBK"));
                    this.QualificationCode =HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, 20)).replaceAll("[\u0000]", "");
                    len = Byte.toUnsignedInt(buf.readByte());
                    this.LicenseIssuing = new String(HexBytesUtil.readByOffset(buf, len), Charset.forName("GBK"));

                    this.CertificateExpiresDate = HexBytesUtil.ReadDateTime_YYMMDD4(HexBytesUtil.readByOffset(buf, 4));
                    this.DriverIdentityCard =HexBytesUtil.parseString(HexBytesUtil.readByOffset(buf, 20)).replaceAll("[\u0000]", "");

                }


            }


        } else {
            //2011版本
        }


        return this;
    }

    public DriverInfo transform() {
        JTT808_0x0702 jtt808_0x0702 = this;
        return new DriverInfo() {{
            msgId = jtt808_0x0702.getPackageHeader().getMsgId();
            terminalPhoneNo=jtt808_0x0702.getPackageHeader().getTerminalPhoneNo();
            IC_Card_Status = jtt808_0x0702.getIC_Card_Status();
            IC_Card_PlugDateTime = jtt808_0x0702.getIC_Card_PlugDateTime();
            IC_Card_ReadResult = jtt808_0x0702.getIC_Card_ReadResult();
            DriverUserName = jtt808_0x0702.getDriverUserName();
            QualificationCode = jtt808_0x0702.getQualificationCode();
            LicenseIssuing = jtt808_0x0702.getLicenseIssuing();
            CertificateExpiresDate = jtt808_0x0702.getCertificateExpiresDate();
            DriverIdentityCard = jtt808_0x0702.getDriverIdentityCard();
        }};
    }


}
